package com.walle.rabbitservice.service.impl;

import com.walle.common.dto.MailDto;
import com.walle.rabbitservice.conf.MailQueueConfig;
import com.walle.rabbitservice.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

/**
 * @author zhangjiapeng
 * @Package com.walle.rabbitservice.service.impl
 * @Description: ${todo}
 * @date 2018/12/27 11:14
 */
@Service
@Slf4j
public class MailServiceImpl implements MailService {

    @Autowired
    private AmqpTemplate amqpTemplate;


    @Override
    public void sendMail(MailDto mailDto) {
        amqpTemplate.convertAndSend(MailQueueConfig.MAIL_EXCHANGE,MailQueueConfig.MAIL_BINDING,mailDto);
    }


    @RabbitHandler
    @RabbitListener(queues = MailQueueConfig.MAIL_QUEUE,concurrency="1")
    public void consumeMailQueue(@Payload MailDto mailDto)  {
        log.info("向用户{}发送邮件{}",mailDto.getUsername(),mailDto.getTitle());
    }
}
